The VectorScript CASE statement lets you specify a list of alternative statements to be executed, associating a constant with each statement to identify it. When the
CASE statement is executed it evaluates the controlling expression, and if the result matches one of the constants, it then executes the associated statement. An optional
OTHERWISE clause allows a different statement to be executed if no other option was selected from the list of constants.
The variable j evaluates to an
INTEGER value, and this value is compared to the list of constants in the
CASE statement. In the example, the value of
j falls outside of the listed constants, so the
OTHERWISE clause is executed.
CASE statements provide some flexibility when specifying constants. For instance, there may be applications of the
CASE statement where several cases will need to execute the same code. Rather than use redundant options, the
CASE statement lets you specify a comma delimited list of constants for a single
CASE option:
For longer contiguous lists of constant values, the CASE statement also supports the use of ranges within the CASE statement constant specification. These ranges specify a contiguous list of constant values to be associated with a statement to be executed:
In the example, it can be seen that the available methods of specifying CASE statement constants provide the ability to specify complex options for branching in a very concise format. Ranges and lists also work with the other supported constant types:
Like other statements, CASE statements can also support the use of compound statements as the controlled statement to be executed. Extending this concept, it is also possible to create nested
CASE statements to handle even more complex branching in scripts: